﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PatientInformationExtract.MainInterface.Views;
using System.Data;
using PatientInformationExtract.MainInterface.Models.XMLCS;


namespace PatientInformationExtract.MainInterface.Views
{
    /// <summary>
    /// Interaction logic for TimeLine.xaml
    /// </summary>
    public partial class TimeLine : UserControl
    {
        internal const double ExpandButtonY = 115;
        internal const double ExpandButtonOriginalX = 78;
        internal const double SecondRowY = 160;
        internal const double IntervalTextBox = 210;
        internal const double IntervalExpandButton = 105;
        internal const double SecondRowOriginalX = 105;
        internal const double SelectedTxtOpacity = 0.6;

        private int _selectedIndex;
        public int SelectedIndex { get { return this._selectedIndex; } }
        private List<TimeLineTextBox> _timeLineTextBoxs;
        private List<ExpandButton> _expandButtons;
        private int _notesCount;
        private List<ProcessNoteUnit> _notes;
        private Rectangle _rectangle;

        public List<ProcessNoteUnit> Source
        {
            set
            {
                this._notesCount = value.Count;
                this._notes = value;
                Initialize();
            }
            get
            {
                return this._notes;
            }
        }

        public int NotesCount { get { return this._notesCount; } }

        public TimeLine()
        {
            InitializeComponent();
            this.Width = 905;
            this.Height = 290;
            this.scrollViewer.Height = this.Height;
            this.scrollViewer.Width = this.Width;
            this.grid.Height = this.Height;
            this.grid.Width = this.Width;
            _rectangle = new Rectangle();
            _timeLineTextBoxs = new List<TimeLineTextBox>();
            _expandButtons = new List<ExpandButton>();
            _notes = new List<ProcessNoteUnit>();
            _selectedIndex = 0;
        }


        /*public static readonly RoutedEvent PreviewTimeLineItemSelectedEvent = EventManager.RegisterRoutedEvent("TimeLineItemSelected", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(TimeLine));
        public static readonly RoutedEvent PreviewTimeLineItemDoubleClickEvent = EventManager.RegisterRoutedEvent("TimeLineItemDoubleClick", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(TimeLine));

        public event RoutedEventHandler PreviewTimeLineItemSelected
        {
            add
            {
                this.AddHandler(PreviewTimeLineItemSelectedEvent, value);
            }
            remove
            {
                this.RemoveHandler(PreviewTimeLineItemSelectedEvent, value);
            }
        }

        public event RoutedEventHandler PreviewTimeLineItemDoubleClick
        {
            add
            {
                this.AddHandler(PreviewTimeLineItemDoubleClickEvent, value);
            }
            remove
            {
                this.RemoveHandler(PreviewTimeLineItemDoubleClickEvent, value);
            }
        }

        private void timeLineTextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            foreach (TimeLineTextBox txt in _timeLineTextBoxs)
            {
                txt.Opacity = 1;
            }
            (sender as TimeLineTextBox).Opacity = SelectedTxtOpacity;
            this._selectedIndex = (sender as TimeLineTextBox).Index;
            this.RaiseEvent(new RoutedEventArgs(PreviewTimeLineItemSelectedEvent));
        }

        private void timeLineTextBox_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this._selectedIndex = (sender as TimeLineTextBox).Index;
            this.RaiseEvent(new RoutedEventArgs(PreviewTimeLineItemDoubleClickEvent));
        }*/

        public delegate void TimeLineItemSelectedHandler(int selectedIndex);
        public event TimeLineItemSelectedHandler TimeLineItemSelectedEvent;

        public delegate void TimeLineItemDoubleClickHandler(int selectedIndex);
        public event TimeLineItemDoubleClickHandler TimeLineItemDoubleClickEvent;

        public void Initialize()
        {
            if (_notesCount > 0)
            {
                this._selectedIndex = 0;
                this.grid.Children.Clear();
                _timeLineTextBoxs = new List<TimeLineTextBox>();
                _expandButtons = new List<ExpandButton>();
                RectangleInitialize();
                ExpandButton btnAll = new ExpandButton(-1);
                this.grid.Children.Add(btnAll);
                btnAll.Height = 12;
                btnAll.Width = 30;
                btnAll.Margin = new Thickness(30, ExpandButtonY + 9, 0, 0);
                btnAll.Checked += new RoutedEventHandler(ExpandAllButton_Checked);
                btnAll.Unchecked += new RoutedEventHandler(ExpandAllButton_Unchecked);
                double txtX_1 = 0;
                double txtX_2 = SecondRowOriginalX;
                double btnX = ExpandButtonOriginalX;
                this.grid.Width = (_notesCount - 1) * 105 + 170;
                for (int i = 0; i < _notesCount; i++)
                {
                    ExpandButton btn = new ExpandButton(i);
                    this.grid.Children.Add(btn);
                    btn.Margin = new Thickness(btnX, ExpandButtonY, 0, 0);
                    btn.Checked += new RoutedEventHandler(ExpandButton_Checked);
                    btn.Unchecked += new RoutedEventHandler(ExpandButton_Unchecked);
                    _expandButtons.Add(btn);
                    btnX += IntervalExpandButton;
                    TimeLineTextBox txt = new TimeLineTextBox(i);
                    if (i == 0)
                    {

                        txt.Type = "入院";
                        txt.Opacity = SelectedTxtOpacity;
                    }
                    else
                    {
                        if (i == (_notesCount - 1))
                        {
                            txt.Type = "出院";
                        }
                        else
                        {
                            txt.Type = "病程";
                        }
                    }
                    if (i % 2 == 0)
                    {
                        txt.Time = _notes[i].Time;
                        txt.TextContent = _notes[i].NoteText;
                        txt.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(txt_PreviewMouseLeftButtonDown);
                        txt.PreviewMouseDoubleClick += new MouseButtonEventHandler(txt_PreviewMouseDoubleClick);
                        this.grid.Children.Add(txt);
                        txt.Margin = new Thickness(txtX_1, 0, 0, 0);
                        _timeLineTextBoxs.Add(txt);
                        txtX_1 += IntervalTextBox;
                    }
                    else
                    {
                        txt.Time = _notes[i].Time;
                        txt.TextContent = _notes[i].NoteText;
                        txt.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(txt_PreviewMouseLeftButtonDown);
                        txt.PreviewMouseDoubleClick += new MouseButtonEventHandler(txt_PreviewMouseDoubleClick);
                        this.grid.Children.Add(txt);
                        txt.Margin = new Thickness(txtX_2, SecondRowY, 0, 0);
                        _timeLineTextBoxs.Add(txt);
                        txtX_2 += IntervalTextBox;
                    }
                }

            }
            else
            {
                throw new Exception("TimeLine source is empty.");
            }
        }

        private void txt_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (TimeLineItemDoubleClickEvent != null)
            {
                this.TimeLineItemDoubleClickEvent((sender as TimeLineTextBox).Index);
            }
            e.Handled = true;
        }

        private void txt_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _timeLineTextBoxs[_selectedIndex].Opacity = 1;
            (sender as TimeLineTextBox).Opacity = SelectedTxtOpacity;
            _selectedIndex = (sender as TimeLineTextBox).Index;
            if (TimeLineItemSelectedEvent != null)
            {
                this.TimeLineItemSelectedEvent((sender as TimeLineTextBox).Index);
                
            }
        }

        private void RectangleInitialize()
        {
            _rectangle = new Rectangle();
            this.grid.Children.Add(_rectangle);
            _rectangle.Width = (_notesCount - 1) * 105;
            _rectangle.Height = 12;
            _rectangle.HorizontalAlignment = HorizontalAlignment.Left;
            _rectangle.VerticalAlignment = VerticalAlignment.Top;
            SolidColorBrush scb = new SolidColorBrush();
            scb.Color = Colors.CadetBlue;
            _rectangle.Fill = scb;
            _rectangle.Margin = new Thickness(85, 124, 0, 0);
        }



        private void ExpandButton_Checked(object sender, RoutedEventArgs e)
        {
            _timeLineTextBoxs[(sender as ExpandButton).Index].Visibility = Visibility.Visible;
        }

        private void ExpandButton_Unchecked(object sender, RoutedEventArgs e)
        {
            _timeLineTextBoxs[(sender as ExpandButton).Index].Visibility = Visibility.Hidden;
        }

        private void ExpandAllButton_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (TimeLineTextBox txt in _timeLineTextBoxs)
            {
                txt.Visibility = Visibility.Hidden;
            }
            foreach (ExpandButton btn in _expandButtons)
            {
                btn.IsChecked = false;
            }
        }

        private void ExpandAllButton_Checked(object sender, RoutedEventArgs e)
        {
            foreach (TimeLineTextBox txt in _timeLineTextBoxs)
            {
                txt.Visibility = Visibility.Visible;
            }
            foreach (ExpandButton btn in _expandButtons)
            {
                btn.IsChecked = true;
            }
        }
    }
}
